//
//  YBSelectPeopleSearchViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBSelectPeopleSearchViewController.h"
#import "YBCustomGroupTableViewCell.h"

#define CELLID @"YBCustomGroupTableViewCell"

@interface YBSelectPeopleSearchViewController ()<UITextFieldDelegate , UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , strong) UITextField * searchTextField;

@property (nonatomic , strong) UIButton * searchButton;

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSArray<YBPeopleInfoModel *> * dataArray;
@end

@implementation YBSelectPeopleSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self ybInitUserInterface];
}

-(void)ybInitUserInterface{
    [self.navigView addSubview:self.searchTextField];
    [self.navigView addSubview:self.searchButton];
    [self.view addSubview:self.tableView];
    
}

-(void)option_searchButton:(UIButton *)sender{
    [self ybBeginSearch];
}


-(void)ybBeginSearch{
    if (self.searchTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入搜索的人员名字" inview:self.view];
        return;
    }
    [self.searchTextField resignFirstResponder];
    [YJProgressHUD showMessage:@"搜索中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    [YBRequestManager request_searchPeopleWithKeyWord:self.searchTextField.text successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            self.dataArray = [NSArray yy_modelArrayWithClass:[YBPeopleInfoModel class] json:object[@"ORG_USERS"]];
            [self.tableView reloadData];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ItemHeight * 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBCustomGroupTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.isSearch = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.dataArray[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(YBSelectPeopleSearchViewControllerDidSelectPeopleModel:)]) {
        [self.delegate YBSelectPeopleSearchViewControllerDidSelectPeopleModel:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self ybBeginSearch];
    return YES;
}


-(UITextField *)searchTextField
{
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, navHeight - 36, [UIScreen zj_screenWith] - 110, 28)];
        UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 37, 28)];
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(12, 4, 20, 20)];
        imageView.image = [UIImage imageNamed:@"nav_sosuo"];
        [contentView addSubview:imageView];
        _searchTextField.leftView = contentView;
        _searchTextField.font = LYPFONT(16);
        _searchTextField.leftViewMode = UITextFieldViewModeAlways;
        _searchTextField.placeholder = @"输入关键字快速查询内容";
        _searchTextField.returnKeyType = UIReturnKeySearch;
        _searchTextField.delegate = self;
        _searchTextField.clipsToBounds = YES;
        _searchTextField.layer.cornerRadius = 14;
        _searchTextField.backgroundColor = [UIColor whiteColor];
    }
    return _searchTextField;
}


-(UIButton *)searchButton
{
    if (!_searchButton) {
        _searchButton = [ToolClass createButtonWithFrame:CGRectMake([UIScreen zj_screenWith] - 55, navHeight - 36, 55, 28) text:@"搜索" textColor:[UIColor whiteColor] imageName:@"" font:20];
        [_searchButton addTarget:self action:@selector(option_searchButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight + ItemHeight * 20, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - ItemHeight * 20 - navHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[YBCustomGroupTableViewCell class] forCellReuseIdentifier:CELLID];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}

@end
